<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <canvas id="canvas" width="400" height="400"></canvas>
  <script>
    /** @type {HTMLCanvasElement} */
    var canvas=document.querySelector("#canvas")
    var ctx=canvas.getContext("2d")
    // ctx.fillRect(0,0,100,100)
    var i=0
    function rend(){
      ctx.clearRect(0,0,400,400)
      i+=1
      if(i>400){
        i=0
      }
      ctx.save()
      ctx.beginPath()
      //不同颜色
      ctx.fillStyle="rgb("+Math.floor(Math.random() * (256)) + 0+","+Math.floor(Math.random() * (256))+","+Math.floor(Math.random() * (256))+")"
      // ctx.translate(i,0)
      ctx.fillRect(i,0,100,100)
      ctx.restore()
      requestAnimationFrame(rend)
    }
    rend()
  </script>
</body>
</html>